﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Socket socketSend;
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                // 创建一个Socket负责建立连接
                socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress iP = IPAddress.Parse(txtClientIP.Text);
                socketSend.Connect(iP, int.Parse(txtClientPort.Text));
                ShowMsg(socketSend.RemoteEndPoint + "连接成功");
                // 创建一个线程，来接收客户端的消息
                Thread th = new Thread(Receive);
                th.IsBackground = true;
                th.Start(socketSend);
            }
            catch { }
        }

        /// <summary>
        /// 接收服务端发送的消息
        /// </summary>
        /// <param name="o"></param>
        public void Receive(object o)
        {
            Socket socketSend = o as Socket;
            while (true)
            {
                try
                {
                    byte[] buffer = new byte[1024 * 1024 * 5];
                    int r = socketSend.Receive(buffer);
                    // 如果是0个字节，就是没有数据传过来，直接放弃
                    if (r == 0)
                    {
                        break;
                    }
                    else// 有字传送过来节，要进行判断是字符还是文件
                    {
                        // 传递的是文字
                        if (buffer[0] == 0)
                        {
                            string str = Encoding.UTF8.GetString(buffer, 1, r - 1);
                            ShowMsg(socketSend.RemoteEndPoint + ":" + str);
                        }
                        // 传递过来的是文件
                        else if (buffer[0] == 1)
                        {
                            SaveFileDialog saveFileDialog = new SaveFileDialog();
                            saveFileDialog.Title = "请选择保存地址";
                            saveFileDialog.Filter = "文本文件|*.txt|所有文件|*.*";
                            saveFileDialog.InitialDirectory = @"C:\Users\CC\Desktop";
                            saveFileDialog.ShowDialog(this);
                            string path = saveFileDialog.FileName;
                            using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                fileStream.Write(buffer, 1, r - 1);
                            }
                            MessageBox.Show("保存成功");
                        }
                        // 震动
                        else if (buffer[0] == 2)
                        {
                            int x = this.Location.X;
                            int y = this.Location.Y;
                            for (int i = 0; i < 1000; i++)
                            {
                                this.Location = new Point(x + 1, y + 1);
                                this.Location = new Point(x - 1, y - 1);
                            }
                            this.Location = new Point(x, y);
                        }
                        else
                        {

                        }
                    }
                }
                catch { }

            }
        }

        public void ShowMsg(string str)
        {
            txtConnectMsg.AppendText(str + "\r\n");
        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] buffer = Encoding.UTF8.GetBytes(txtSendMsg.Text);
                socketSend.Send(buffer);
            }
            catch { }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}
